/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.gui.sales;

import com.kimstore.bean.Item;
import com.kimstore.bean.PurchaseDetail;
import com.kimstore.factory.DaoFactory;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class AddItemToOrderController {

    private NewOrderController parentController;
    private AddItemToOrderWindow addItemWindow;
    private Item selectedItem;
    private ArrayList<Item> freebies = null;

    public AddItemToOrderController(NewOrderController newOrderController) {
        this.parentController = newOrderController;
        freebies = new ArrayList<Item>();
    }

    public void displayWindow() {
        addItemWindow = new AddItemToOrderWindow(this);
        addItemWindow.showWindow();
    }

    public void fromWindow() {
        if(addItemWindow.getDiscountField().getText().isEmpty())
            addItemWindow.getDiscountField().setText("0.00");
        
        Float discount = Float.parseFloat(addItemWindow.getDiscountField().getText());
        Float basePrice = Float.parseFloat(addItemWindow.getBasePriceField().getText());

        PurchaseDetail purchaseDetail = new PurchaseDetail();
        
        purchaseDetail.setDiscount(discount);
        purchaseDetail.setPriceSold(basePrice - discount);
        
        selectedItem.setPurchaseDetail(purchaseDetail);

        for(Item item: freebies) {
            PurchaseDetail pd = new PurchaseDetail();
            pd.setDiscount(item.getProduct().getBasePrice());
            pd.setPriceSold(0);
            
            item.setPurchaseDetail(pd);
        }
        
        this.parentController.createAddItemControllerCallback(selectedItem, freebies);
    }

    void createSelectItemController() {
        SelectItemControllerX selectItemController = new SelectItemControllerX(this);
        selectItemController.displayWindow();
    }

    public void createSelectItemControllerCallback(Item item) {
        this.selectedItem = item;

        addItemWindow.getProductDetailsField().setText(selectedItem.getProduct().getName());
        addItemWindow.getBasePriceField().setText("" + selectedItem.getProduct().getBasePrice());
        addItemWindow.getDiscountField().setText("0.00");
    }
    
    public void createAddFreebieToItemController(){
        AddFreebieToItemController controller = new AddFreebieToItemController(this);
        controller.displayWindow();
    }
    
    public List<Item> getAllSelectedItems() {
        List<Item> remove = new ArrayList<Item>();
        
        if(parentController.getItems() != null)
            remove.addAll(parentController.getItems());
        
        if(selectedItem != null)
            remove.add(selectedItem);
        
        if(freebies.size() > 0)
            remove.addAll(freebies);
        
        return remove;
    }

    void createAddFreebieToItemControllerCallback(String sku) {
        Item freebie = DaoFactory.getInstance().getItemDao().findBySerial(sku);
        
        freebies.add(freebie);
        
        addItemWindow.populateFreebiesTable(freebies);
    }

    void removeItem(String sku) {
        for(Item free: freebies) {
            if(free.getSerial().equals(sku)) {
                freebies.remove(free);
                break;
            }
        }
        
        
    }
    
    public void validate() throws Error {
        if(selectedItem == null) {
            throw new Error("<html>Please select an <b>Item</b></html>");
        }
        
        if(addItemWindow.getBasePriceField().getText().isEmpty()) {
            throw new Error("<html>Please fill up the <b>Selling Price</b> field</html>");
        }
    }
}
